<?php

require_once("{$GLOBALS['srcdir']}/sql.inc");

//Funcion para ver si existe la conexion paciente medico.
function existePacienteMedico($provider_id, $pid) {
    $res = sqlStatement("select * from patient_provider where provider_id={$provider_id} and pid={$pid}");
    $row = sqlFetchArray($res);
    if (isset($row["id"]))
        return 1;
    else
        return 0;
}

function existeAutorizacionMedicoConsultaPacientes($provider_id) {
    if(!isset($provider_id))
        return 0;
    $res = sqlStatement("select * from users where id={$provider_id} and details_auth_by_provider = 1");
    $row = sqlFetchArray($res);
    if (isset($row["id"]))
        return 1;
    else
        return 0;
}

function existeAutorizacionMedicoConsultaPacientesUsuario($provider) {
    if(!isset($provider))
        return 0;
    $res = sqlStatement("select * from users where username='{$provider}' and details_auth_by_provider = 1");
    $row = sqlFetchArray($res);
    if (isset($row["id"]))
        return 1;
    else
        return 0;
}

function existeAutorizacionMedicoInfoPacientes($provider_id) {
    if(!isset($provider_id))
        return 0;
    $res = sqlStatement("select * from users where id={$provider_id} and providers_auth_by_provider = 1");
    $row = sqlFetchArray($res);
    if (isset($row["id"]))
        return 1;
    else
        return 0;
}

function existeAutorizacionMedicoInfoPacientesUsuario($provider) {
    if(!isset($provider))
        return 0;
    $res = sqlStatement("select * from users where username='{$provider}' and providers_auth_by_provider = 1");
    $row = sqlFetchArray($res);
    if (isset($row["id"]))
        return 1;
    else
        return 0;
}

function existeAutorizacionSeccionesPaciente($provider_id, $pid) {
    if(!isset($provider_id))
        return 1;
    $res = sqlStatement("select * from patient_provider where provider_id={$provider_id} and pid={$pid} and details_auth_by_patient = 1");
    $row = sqlFetchArray($res);
    if (isset($row["id"]))
        return 1;
    else
        return 0;
}

function existeAutorizacionDatosGeneralesPaciente($provider_id, $pid) {
    $res = sqlStatement("select * from patient_provider where provider_id={$provider_id} and pid={$pid} and patient_general_data = 1");
    $row = sqlFetchArray($res);
    if (isset($row["id"]))
        return 1;
    else
        return 0;
}

function crearRelacionPacienteMedico($provider_id, $pid, $medico_creador=false) {
    
    //echo "provider_id ".$provider_id." pid = ". $pid; die;
    
    $sql = "INSERT INTO patient_provider SET " .
            "created_date = CURDATE(), " .
            "last_update = CURDATE(), " .
            "pid = '$pid', " .
            "provider_id = '$provider_id'";
    
    if($medico_creador){
        
        $sql .= ", patient_general_data = 1";
    }
    
    //echo $sql;die;
    
    sqlInsert($sql);
    //Insert into bitacora
    off_sync($sql);
    
}

function existePacienteMedicoVisita($id) {
    $res = sqlStatement("select * from patient_provider_encounter where encounter_id={$id}");
    $row = sqlFetchArray($res);
    if (isset($row["id"]))
        return 1;
    else
        return 0;
}

function existeAutorizacionVisita($id) {
    $res = sqlStatement("select * from patient_provider_encounter where encounter_id={$id} and 	encounter_auth_by_patient = 1");
    $row = sqlFetchArray($res);
    if (isset($row["id"]))
        return 1;
    else
        return 0;
}

function crearRelacionPacienteMedicoVisita($id) {
    $query = "INSERT INTO patient_provider_encounter SET " .
            "created_date = CURDATE(), " .
            "last_update = CURDATE(), " .
            "encounter_id = '$id'";
    sqlInsert($query);
    //Insert into bitacora
    off_sync($query);
}

?>
